package logintest.servlet;

import logintest.bean.User;
import logintest.dao.UserDao;
import logintest.dao.impl.UserDaoImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/getUid")
public class UidServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String uname = req.getParameter("uname");
        //因为要把字段返回，所以要做乱码处理
        resp.setContentType("text/html; charset=utf-8");
        //创建writer对象，往前台写入字符串
        PrintWriter writer = resp.getWriter();
        //连接数据库验证uname是否存在
        UserDao userDao = new UserDaoImpl();
        User user = userDao.UnameTest(uname);
        /*
         * 根据用户名展示用户其他信息
         * */
        JSONObject jsonObject = JSONObject.fromObject(user);
        writer.print(jsonObject);
    }
}
